package com.itheima.controller.backend;


import com.itheima.dto.OrderSettingDTO;
import com.itheima.common.entity.Result;
import com.itheima.service.backend.OrdersettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 预约设置管理
 */
@RestController
@RequestMapping( "/ordersetting")
@Slf4j
public class OrdersettingController {

    @Autowired
    private OrdersettingService orderSettingService;

    /**
     * 下载预约设置数据模板
     * @param response
     * @return
     */
    @GetMapping("/download")
    public Result download(HttpServletResponse response){
        log.info("下载预约设置数据模板");
        try {
            orderSettingService.download(response);
            return new Result(true, "下载成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "下载失败");
        }
    }

    /**
     * 上传预约设置数据
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result uploadExcel(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            log.info("上传预约设置数据：{}", excelFile);
            orderSettingService.saveExcelDataToDatabase(excelFile);
            return new Result(true, "数据导入成功");
        } catch (Exception e) {
            return new Result(false, "数据导入失败");
        }
    }

    /**
     * 根据月份查询预约设置数据
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        log.info("根据月份查询预约设置数据：{}", month);
        try {
            return new Result(true, "查询成功",orderSettingService.getOrderSettingByMonth(month));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 根据预约日期设置每天可预约人数
     * @param orderSettingDTO
     * @return
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSettingDTO orderSettingDTO){
        log.info("根据预约日期设置每天可预约人数：{}", orderSettingDTO);
        try {
            orderSettingService.editNumberByOrderDate(orderSettingDTO);
            return new Result(true, "编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑失败");
        }
    }
 }
